const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const {CleanWebpackPlugin} = require("clean-webpack-plugin");
const CopyWebpackPlugin = require('copy-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');

const cesiumSource = 'src/Cesium';
const cesiumWorkers = 'Workers';
const cesiumJs = 'Cesium.js';
const CEarthSource = 'src/CEarth';

const config = {
	entry: {
		"CEarth": "./src/CEarth/js/CEarth.js",
		"CEarth.min": "./src/CEarth/js/CEarth.js",
	},
	output: {
		path: path.resolve(__dirname,  'build'),
		filename: 'CEarth/[name].js',
		library: "CEarth",
		libraryExport: "default", // 对外暴露default属性，就可以直接调用default里的属性
		globalObject: 'this', // 定义全局变量,兼容node和浏览器运行，避免出现"window is not defined"的情况
		libraryTarget: 'umd', // 定义打包方式Universal Module Definition,同时支持在CommonJS、AMD和全局变量使用
		sourcePrefix: ''
	},
	amd: {
		toUrlUndefined: true
	},
	resolve: {
		fallback: {
			fs: false
		},
		alias: {
			cesium: path.resolve(__dirname, cesiumSource),
		}
	},
	devtool: 'cheap-source-map',
	module: {
		rules: [{
                test: /\.css$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader']
            },
			{
				test: /\.(png|gif|jpg|jpeg|svg|xml|json)$/,
				use: ['url-loader']
			},
			{
				test: /\.js/,
				include: [
					path.resolve(__dirname, 'src')
				],
				exclude: /node_modules/, 
				use: [{
					loader: 'babel-loader'
				}]
			}
		]
	},
	optimization: {
/* 		splitChunks: {
            chunks: 'all',
            minSize: 50000,
            maxSize: 0,
        },	 */
		minimizer: [
			new TerserPlugin({
				include: /\.min\.js$/,
			}),
		]
	},
	plugins: [
		new CopyWebpackPlugin({
			patterns: [{
				from: path.join(CEarthSource, 'css'),
				to: 'CEarth'
			}],
		}),		
		/*****************Cesium*******************/
		new CopyWebpackPlugin({
			patterns: [{
				from: path.join(cesiumSource, cesiumWorkers),
				to: 'Cesium/Workers',
			}],
		}),
		new CopyWebpackPlugin({
			patterns: [{
				from: path.join(cesiumSource, cesiumJs),
				to: 'Cesium',
			}],
		}),
		new CopyWebpackPlugin({
			patterns: [{
				from: path.join(cesiumSource, 'Assets'),
				to: 'Cesium/Assets'
			}],
		}),
		new CopyWebpackPlugin({
			patterns: [{
				from: path.join(cesiumSource, 'ThirdParty'),
				to: 'Cesium/ThirdParty'
			}],
		}),		
		new CopyWebpackPlugin({
			patterns: [{
				from: path.join(cesiumSource, 'Widgets'),
				to: 'Cesium/Widgets'
			}],
		}),
		new webpack.DefinePlugin({
			CESIUM_BASE_URL: JSON.stringify('')
		})
	],
	mode: 'production',
	devServer: {
		contentBase: path.resolve(__dirname, './'),
		watchContentBase: true,
		watchOptions: {
			ignored: /node_modules/
		},
		openPage:'examples/index.html',
		compress: true,
		inline:true,
		open: true,
		hot: true,
		clientLogLevel: 'none',
		quiet: true,
		overlay: false,
	}
};
module.exports = config;
